package com.qf.mpswaggershiro.shopcart.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.mpswaggershiro.product.entity.Product;
import com.qf.mpswaggershiro.product.entity.ProductImg;
import com.qf.mpswaggershiro.product.entity.ProductSku;
import com.qf.mpswaggershiro.product.service.IProductImgService;
import com.qf.mpswaggershiro.product.service.IProductService;
import com.qf.mpswaggershiro.product.service.IProductSkuService;
import com.qf.mpswaggershiro.shopcart.entity.ShoppingCart;
import com.qf.mpswaggershiro.shopcart.mapper.ShoppingCartMapper;
import com.qf.mpswaggershiro.shopcart.service.IShoppingCartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 购物车  服务实现类
 * </p>
 *
 * @author cui
 * @since 2023-02-16
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements IShoppingCartService {
@Autowired
    IProductService productService;
@Autowired
IProductImgService productImgService;
@Autowired
IProductSkuService productSkuService;

@Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<ShoppingCart> listShoppingCart(Integer userId) {


        //通过给的这个userid拿到`shopping_cart`表中的所有数据
        List<ShoppingCart> shopcartList = list(new QueryWrapper<ShoppingCart>().eq("user_id", userId));

       //2.补全 实体类entity中的shoppingCart 对象中后添加的的数据
        populateCart(shopcartList);


        return shopcartList;
    }

    /**
     * 结算页面查询接口
     * @param cids
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public List<ShoppingCart> listCids(String cids) {
//用，分割成数组
        String[] split = cids.split(",");

        List<ShoppingCart> shoppingCarts = list(new QueryWrapper<ShoppingCart>().in("cart_id", Arrays.asList(split)));

        populateCart(shoppingCarts);

        return shoppingCarts;
    }

    private void populateCart(List<ShoppingCart> shopcartList) {

        //1.查询商品表
        List<String> productIdList = shopcartList.stream().map(ShoppingCart::getProductId).collect(Collectors.toList());
        //2.拿到product表中的所有符合条件的id和name
        List<Product> productList = productService.list(new QueryWrapper<Product>().in("product_id", productIdList));
        // 用map存储 value存储name
        Map<String, String> productIdNameMap = productList.stream().collect(Collectors.toMap(Product::getProductId, Product::getProductName));
        //查询商品图片表
        List<ProductImg> productImgList = productImgService.list(new QueryWrapper<ProductImg>().eq("is_main", 1).in("item_id", productIdList));
        //转化成map value存储url
        Map<String, String> productImgUrlMap = productImgList.stream().collect(Collectors.toMap(ProductImg::getItemId, ProductImg::getUrl));
        //查询sku表
        //1. List<ShoppingCart>  ---> List<SkuId>  拿到skuid的list集合
        List<String> skuIdList = shopcartList.stream().map(ShoppingCart::getSkuId).collect(Collectors.toList());
        //2.根据id查询sku表中数据
        List<ProductSku> skuList = productSkuService.list(new QueryWrapper<ProductSku>().in("sku_id", skuIdList));
        //3.转换成map value是sku对象
        Map<String, ProductSku> productSkuMap = skuList.stream().collect(Collectors.toMap(ProductSku::getSkuId, productSku -> productSku));
        //遍历所有把数据放进shopcartList中
        for (ShoppingCart shoppingCart : shopcartList) {
            String productId = shoppingCart.getProductId();
            String productName = productIdNameMap.get(productId);
            String productImg = productImgUrlMap.get(productId);
            //skuid是通过shoppingCart拿到的，所以需要用它去调
            ProductSku productSku = productSkuMap.get(shoppingCart.getSkuId());
            shoppingCart.setProductName(productName);
            shoppingCart.setProductImg(productImg);
            shoppingCart.setOriginalPrice(productSku.getOriginalPrice());
            shoppingCart.setSellPrice(productSku.getSellPrice());
            shoppingCart.setStock(productSku.getStock());
        }

    }
}
